import {
  Shader,
  ShaderData,
  ShaderProperty,
  ShaderPool,
  ShaderProgram,
  ShaderProgramPool,
  ShaderMacro,
  ShaderMacroCollection,
  ShaderDataGroup,
  ShaderLib
} from "@nirvana/shaders";
import { Vector3, Matrix4 } from "@nirvana/math";

import { WebGLCanvas } from "@nirvana/canvas";
import { VertexBufferData, IndexBufferData, DataElement } from "@nirvana/core";

const shaderMacroCollection: ShaderMacroCollection = new ShaderMacroCollection();
console.log(shaderMacroCollection);
const colorShaderData: ShaderData = new ShaderData(ShaderDataGroup.Material);
console.log(ShaderDataGroup, colorShaderData);
const pool: ShaderProgramPool = new ShaderProgramPool();

// 使用使用 Canvas
let canvas = document.getElementById("engine-nirvana");
const webCanvas = new WebGLCanvas(canvas, {});
const engine = webCanvas.engine;
const gl = engine.gl;
const cVS = `
attribute vec3 position;
uniform mat4 rotateMatrix;
varying vec3 vColor;
void main(void) { 
  gl_Position = rotateMatrix * vec4(position, 1.);
  vColor = vec3(1.0,1.0,1.0);
}
`;
const cFS = `
varying vec3 vColor;

void main(void) {
  gl_FragColor = vec4(vColor, 1.0);
}`;
const colorDemo = Shader.create("colorDemo", cVS, cFS);
console.log(colorDemo, Shader.find("colorDemo"));

const program: ShaderProgram = colorDemo.getShaderProgram(engine.gl, shaderMacroCollection, pool);
console.log("color", program);

const vertices: number[] = [
  -0.5, -0.5, -0.5,  0.5, -0.5,-0.5, 0.5, 0.5,-0.5, -0.5, 0.5,-0.5,
  -0.5, -0.5,  0.5,  0.5, -0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5,
  -0.5, -0.5, -0.5, -0.5, 0.5,-0.5, -0.5, 0.5, 0.5, -0.5,-0.5, 0.5,
   0.5, -0.5, -0.5,  0.5, 0.5,-0.5, 0.5, 0.5, 0.5, 0.5,-0.5, 0.5,
  -0.5, -0.5, -0.5, -0.5,-0.5, 0.5, 0.5,-0.5, 0.5, 0.5,-0.5,-0.5,
  -0.5,  0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,-0.5, 
];

const indices: number[] = [
  0,1,2, 0,2,3, 4,5,6, 4,6,7,
  8,9,10, 8,10,11, 12,13,14, 12,14,15,
  16,17,18, 16,18,19, 20,21,22, 20,22,23
];

let nIndices: number[] = [];
if (indices.length > 0) {
  for (let i = 0, len = indices.length; i < len; i += 3) {
    const a = indices[i + 0];
    const b = indices[i + 1];
    const c = indices[i + 2];

    nIndices.push(a, b, b, c, c, a);
  }
} else {
  for (let i = 0, l = vertices.length / 3 - 1; i < l; i += 3) {
    const a = i + 0;
    const b = i + 1;
    const c = i + 2;

    nIndices.push(a, b, b, c, c, a);
  }
}

// nIndices = indices;

console.log(nIndices);
const vBuffer = new VertexBufferData(new Float32Array(vertices), [
  new DataElement("position", 3, gl.FLOAT, false, 0, 0)
]);

const iBuffer = new IndexBufferData(new Uint16Array(nIndices));
vBuffer.create(gl);
iBuffer.create(gl);

const shaderData: ShaderData = new ShaderData(ShaderDataGroup.Material);
console.log(ShaderDataGroup, shaderData);

let r = 0;
const axis = new Vector3(0.8, 1.0, 0.5);
const rotateMatrix = Matrix4.e;

engine.update = (gl) => {
  r += 0.01;
  rotateMatrix.rotationAxisAngle(axis, r);
  // console.log('--');
  gl.clearColor(0.0, 0.0, 0.0, 1.0);
  // 用上面指定的颜色清除缓冲区
  gl.clear(gl.COLOR_BUFFER_BIT);
  gl.enable(gl.DEPTH_TEST);
  // gl.disable(gl.DEPTH_WRITEMASK);
  // 设置混合函数
  // gl.enable(gl.BLEND);
  // gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
  // gl.enable(gl.CULL_FACE);
  // 颜色 start 三
  program.bind();
  vBuffer.upload(program._glProgram, gl);
  shaderData.setMatrix("rotateMatrix", rotateMatrix);
  program.uploadAll(program.otherUniformBlock, shaderData);
  iBuffer.upload(gl);
  // gl.drawArrays(gl.LINES, 0, nIndices.length / 3);
  gl.drawElements(gl.LINES, nIndices.length, gl.UNSIGNED_SHORT, 0);
  // iBuffer.draw(gl);
};
